﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post.Persistence;
using NHibernate;
using WuDada.Core.Generic.Util;
using WuDada.Core.Member.Domain;

namespace WuDada.Core.Post.Service.Impl
{
    public class AnswerService : IAnswerService
    {
        public IAnswerDao AnswerDao { get; set; }
        public IAnswerDetailDao AnswerDetailDao { get; set; }
        public IQuestionDao QuestionDao { get; set; }
        public IPostDao PostDao { get; set; }

        /// <summary>
        /// 新增Answer
        /// </summary>
        /// <param name="answerVO">被新增的Answer</param>
        /// <returns>新增後的Answer</returns>
        public AnswerVO CreateAnswer(AnswerVO answerVO)
        {
            answerVO = AnswerDao.CreateAnswer(answerVO);
            if (answerVO.SortNo == 0)
            {
                answerVO.SortNo = answerVO.AnswerId;
                answerVO = AnswerDao.UpdateAnswer(answerVO);
            }
            return answerVO;
        }

        /// <summary>
        /// 取得Answer By AnswerId
        /// </summary>
        /// <param name="answerId">AnswerId</param>
        /// <returns>Answer</returns>
        public AnswerVO GetAnswerById(int answerId)
        {
            return AnswerDao.GetAnswerById(answerId);
        }

        /// <summary>
        /// 刪除Answer
        /// </summary>
        /// <param name="answerVO">被刪除的Answer</param>
        public void DeleteAnswer(AnswerVO answerVO)
        {
            AnswerDao.DeleteAnswer(answerVO);
        }

        /// <summary>
        /// 更新Answer
        /// </summary>
        /// <param name="answerVO">被更新的Answer</param>
        /// <returns>更新後的Answer</returns>
        public AnswerVO UpdateAnswer(AnswerVO answerVO)
        {
            return AnswerDao.UpdateAnswer(answerVO);
        }

        /// <summary>
        /// 取得Answer清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>Answer清單</returns>
        public IList<AnswerVO> GetAnswerList(IDictionary<string, string> conditions)
        {
            return AnswerDao.GetAnswerList(conditions);
        }

        /// <summary>
        /// 取得Answer數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetAnswerCount(IDictionary<string, string> conditions)
        {
            return AnswerDao.GetAnswerCount(conditions);
        }

        /// <summary>
        /// 新增AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被新增的AnswerDetail</param>
        /// <returns>新增後的AnswerDetail</returns>
        public AnswerDetailVO CreateAnswerDetail(AnswerDetailVO answerDetailVO)
        {
            answerDetailVO = AnswerDetailDao.CreateAnswerDetail(answerDetailVO);
            if (answerDetailVO.SortNo == 0)
            {
                answerDetailVO.SortNo = answerDetailVO.AnswerDetailId;
                answerDetailVO = AnswerDetailDao.UpdateAnswerDetail(answerDetailVO);
            }
            return answerDetailVO;
        }

        /// <summary>
        /// 取得AnswerDetail By AnswerDetailId
        /// </summary>
        /// <param name="answerDetailId">AnswerDetailId</param>
        /// <returns>AnswerDetail</returns>
        public AnswerDetailVO GetAnswerDetailById(int answerDetailId)
        {
            return AnswerDetailDao.GetAnswerDetailById(answerDetailId);
        }

        /// <summary>
        /// 刪除AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被刪除的AnswerDetail</param>
        public void DeleteAnswerDetail(AnswerDetailVO answerDetailVO)
        {
            AnswerDetailDao.DeleteAnswerDetail(answerDetailVO);
        }

        /// <summary>
        /// 更新AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被更新的AnswerDetail</param>
        /// <returns>更新後的AnswerDetail</returns>
        public AnswerDetailVO UpdateAnswerDetail(AnswerDetailVO answerDetailVO)
        {            
            return AnswerDetailDao.UpdateAnswerDetail(answerDetailVO);
        }

        /// <summary>
        /// 取得AnswerDetail清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>AnswerDetail清單</returns>
        public IList<AnswerDetailVO> GetAnswerDetailList(IDictionary<string, string> conditions)
        {
            return AnswerDetailDao.GetAnswerDetailList(conditions);
        }

        /// <summary>
        /// 取得AnswerDetail數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetAnswerDetailCount(IDictionary<string, string> conditions)
        {
            return AnswerDetailDao.GetAnswerDetailCount(conditions);
        }

        /// <summary>
        /// 產生一個空白的問卷回答
        /// </summary>
        /// <param name="postId">關聯的主題Id</param>
        /// <param name="needQuestionNum">需要幾個題目</param>
        /// <param name="memberVO">會員</param>
        /// <returns></returns>
        public AnswerVO CreateEmptyAnswer(int postId, int needQuestionNum, MemberVO memberVO)
        {
            PostVO postVO = PostDao.GetPostById(postId);
            if (postVO == null || postVO.Flag != 1)
            {
                return null;
            }

            IDictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("PostId", postId.ToString());
            conditions.Add("Flag", "1");

            int questionCount = QuestionDao.GetQuestionCount(conditions);
            if (questionCount >= needQuestionNum)
            {
                //建立一個AnswerVO
                AnswerVO answerVO = new AnswerVO();
                answerVO.Member = memberVO;
                answerVO.CreatedDate = DateTime.Now;
                answerVO.Flag = 0;
                answerVO.PostId = postId;
                answerVO.PostTitle = postVO.Title;
                answerVO.Result = 0;
                AnswerDao.CreateAnswer(answerVO);

                IList<QuestionVO> questionList = QuestionDao.GetQuestionList(conditions);
                IList<int> list = new List<int>(questionList.Count());
                for (int i = 0; i < questionList.Count(); i++)
                {
                    list.Add(i);
                }
                int[] ramdonArray = list.ToArray();
                ramdonArray = ConvertUtil.ShuffleArray(ramdonArray);

                for (int i = 0; i < needQuestionNum; i++)
                {
                    QuestionVO questionVO = questionList[ramdonArray[i]];

                    ////建立要做的題目
                    AnswerDetailVO answerDetailVO = new AnswerDetailVO();
                    answerDetailVO.Answer = answerVO;
                    answerDetailVO.Flag = 1;
                    answerDetailVO.CreatedDate = DateTime.Now;

                    answerDetailVO.Question = questionVO.Question;                    
                    answerDetailVO.Option1 = questionVO.Option1;
                    answerDetailVO.Option2 = questionVO.Option2;
                    answerDetailVO.Option3 = questionVO.Option3;
                    answerDetailVO.Option4 = questionVO.Option4;
                    answerDetailVO.CorrectAnswer = questionVO.CorrectAnswer;
                    answerDetailVO.CanScore = questionVO.CanScore;

                    answerDetailVO.SortNo = i;
                    AnswerDetailDao.CreateAnswerDetail(answerDetailVO);                    
                }

                return answerVO;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 完成問卷
        /// </summary>
        /// <param name="answerVO"></param>
        /// <param name="passScore">及格分數</param>
        /// <returns></returns>
        public AnswerVO UpdateFinishAnswer(AnswerVO answerVO, double passScore)
        {
            double totalScore = 0;

            IDictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("AnswerId", answerVO.AnswerId.ToString());
            conditions.Add("Flag", "1");

            IList<AnswerDetailVO> answerDetailList = AnswerDetailDao.GetAnswerDetailList(conditions);
            if (answerDetailList != null && answerDetailList.Count > 0)
            {
                foreach (AnswerDetailVO answerDetailVO in answerDetailList)
                {
                    answerDetailVO.IsCorrect = false;
                    answerDetailVO.Score = 0;
                    if (!string.IsNullOrEmpty(answerDetailVO.UserAnswer) && !string.IsNullOrEmpty(answerDetailVO.CorrectAnswer))
                    {
                        answerDetailVO.IsCorrect = answerDetailVO.UserAnswer.Equals(answerDetailVO.CorrectAnswer);
                        if (answerDetailVO.IsCorrect)
                        {
                            answerDetailVO.Score = answerDetailVO.CanScore;
                            totalScore += answerDetailVO.Score;
                        }
                    }
                    answerDetailVO.UpdatedDate = DateTime.Now;        
                    AnswerDetailDao.UpdateAnswerDetail(answerDetailVO);
                }
            }

            answerVO.TotalScore = totalScore;
            if (answerVO.TotalScore >= passScore)
            {
                answerVO.Result = 1;
            }
            else
            {
                answerVO.Result = 2;
            }
            answerVO.UpdatedDate = DateTime.Now;
            answerVO.Flag = 1;
            answerVO = AnswerDao.UpdateAnswer(answerVO);

            return answerVO;
        }
    }
}
